//
//  LAGClusterAnnotation.m
//  2bulu-QuanZi
//
//  Created by 罗亮富 on 14-8-16.
//  Copyright (c) 2014年 Lolaage. All rights reserved.
//

#import "LAGClusterAnnotation.h"

@interface LAGClusterAnnotation ()

@end

@implementation LAGClusterAnnotation
//@synthesize childAnnotations = _childAnnotations;
@synthesize clusterGroupID = _clusterGroupID;



-(id)initWithChildAnnotations:(NSArray<id<MKAnnotation>> *)childAnnotations groupIdentifier:(nonnull NSString *)groupIdentifier
{
    if(!childAnnotations || !groupIdentifier)
        return nil;
    
    self = [super init];
    if(self)
    {
        _clusterGroupID = groupIdentifier;
        self.childAnnotations = childAnnotations;
    }
    return self;
}

-(void)updateCoordinate
{
//    if(_positionMethod == ClusterPositionAtChildrenCenter)
//        self.coordinate = [[self class] centerCoordinateForAnnotations:_childAnnotations];
//    else
        self.coordinate = _childAnnotations.firstObject.coordinate;
}

-(void)setChildAnnotations:(nonnull NSArray<id<MKAnnotation>> *)childAnnotations
{
    if(childAnnotations.count == 0)
        return;
    
    _childAnnotations = [NSMutableArray arrayWithArray:childAnnotations];
    [self updateCoordinate];
}

-(NSArray *)childAnnotations
{
    if(_childAnnotations.count > 0)
        return [NSArray arrayWithArray:_childAnnotations];
    else
        return nil;
}

-(void)addChildAnnotations:(NSArray <id<MKAnnotation>> *)annotations
{
    if(annotations.count == 0)
        return;
    
    [_childAnnotations addObjectsFromArray:annotations];
    [self updateCoordinate];
}


-(void)removeChildAnnotations:(nonnull NSArray <id<MKAnnotation>> *)annotations
{
    if(annotations.count == 0)
        return;
    
    [_childAnnotations removeObjectsInArray:annotations];
    [self updateCoordinate];
}

-(void)setFrontChilldAnnotation:(id<MKAnnotation>)frontChildAnnotation
{
    _frontChildAnnotation = frontChildAnnotation;
    if(![_childAnnotations containsObject:frontChildAnnotation])
        return;
    NSUInteger index = [_childAnnotations indexOfObject:frontChildAnnotation];
    if(index>0)
        [_childAnnotations exchangeObjectAtIndex:index withObjectAtIndex:0];//moveObjectFromIndex:index toIndex:0];
}

-(id<MKAnnotation>)frontChildAnnotation
{
    if(!_frontChildAnnotation)
    {
        _frontChildAnnotation = _childAnnotations.firstObject;
    }
    return _frontChildAnnotation;
}

//+(CLLocationCoordinate2D)centerCoordinateForAnnotations:(NSArray<id<MKAnnotation>> *)annotations
//{
//    
//    for(id<MKAnnotation> in annotations)
//    {
//        
//    }
//}



@end
